package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QuoteException;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = SetMealService.class)
@Slf4j
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    //分页查询套餐数据
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = PageHelper.startPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize())
                .doSelectPage(() -> setMealDao.findPage(queryPageBean.getQueryString()));
        return new PageResult(page.getTotal(), page.getResult());
    }

    //查询单个套餐信息
    @Override
    public Result findById(Integer id) {
        try {
            Setmeal setmeal = setMealDao.findById(id);
            if (setmeal != null) {
                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
    }

    //查询所有的套餐
    @Override
    public List<Setmeal> findAll() {
       return  setMealDao.findAll();

    }

    //根据套餐id查询关联表中检查组的id数据
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {

        List<Integer> setMealIds = setMealDao.findCheckGroupIdsBySetmealId(id);
        if (CollectionUtils.isNotEmpty(setMealIds)) {
            return setMealIds;
        }
        return null;

    }


    //添加套餐信息
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        if (!ObjectUtil.isAllNotEmpty(setmeal.getCode(), setmeal.getName(), checkgroupIds)) {
            throw new QuoteException();
        }
        setMealDao.add(setmeal);
        if (setmeal.getId() == null) {
            throw new QuoteException(MessageConstant.ADD_SETMEAL_FAIL);
        }
        //添加组和套餐关系表的关联
        setSetMealCheckGroup(setmeal.getId(), Arrays.asList(checkgroupIds));
        //将七牛云的图片素材设置到1年后清理
        QiniuUtils.deleteFileFromQiniu(setmeal.getImg(), 365 * 1);


    }

    // //添加组和套餐关系表的关联
    private void setSetMealCheckGroup(Integer setMealId, List<Integer> addList) {
        Map<String, Object> map = new HashMap<>();
        map.put("setMealId", setMealId);
        map.put("checkGroupIds", addList);
        setMealDao.setSetMealCheckGroup(map);
    }

    //删除套餐
    @Override
    public void delete(Integer id) {


        //查到当前要删除的套餐
        Setmeal setmeal = setMealDao.findById(id);
        //解除套餐和组的关联关系
        setMealDao.deleteSetMealAndCheckGroup(id);
        //查询之后再进行删除
        setMealDao.deleteById(id);
        //删除七牛云上的图片
        QiniuUtils.deleteFileFromQiniu(setmeal.getImg());

    }

    //编辑当前套餐表
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        if (!ObjectUtil.isAllNotEmpty(setmeal.getId(), setmeal.getCode(), setmeal.getName(), checkgroupIds)) {
            throw new QuoteException();
        }
        //先查询之前老的套餐
        Setmeal olds = setMealDao.findById(setmeal.getId());
        //更新当前套餐表
        setMealDao.edit(setmeal);
        List<Integer> newCheckGroupIds = Arrays.asList(checkgroupIds);//吧新的检查组id放入一个集合中

        List<Integer> oldCheckGroupIds = setMealDao.findCheckGroupIdsBySetmealId(setmeal.getId());//根据当前套餐的id,查询出老的检查组id
        ArrayList<Integer> addList = new ArrayList<>(newCheckGroupIds);
        addList.removeAll(oldCheckGroupIds);
        //添加新的关联字段
        setSetMealCheckGroup(setmeal.getId(), addList);

        ArrayList<Integer> delList = new ArrayList<>(oldCheckGroupIds);
        delList.removeAll(newCheckGroupIds);
        //解除老的关联字段
        setMealDao.batchDelByCheckGroupId(delList);

        /*//解除套餐和组的关联关系
        setMealDao.deleteSetMealAndCheckGroup(setmeal.getId());
        //批量更新套餐和组关联表的字段信息
        setMealDao.setSetMealCheckGroup(setmeal.getId(), checkgroupIds);*/
        //放到最后,保证所有的sql语句执行完成,不会回滚
        if (olds.getImg() != null && olds.getImg().equals(setmeal.getImg())) {
            QiniuUtils.deleteFileFromQiniu(olds.getImg(), 1);
            QiniuUtils.deleteFileFromQiniu(setmeal.getImg(), 1 * 365);
        }


    }

}
